//
//  hotView.m
//  hot
//
//  Created by calumr on Sat Jul 07 2001.
//  Copyright (c) 2001 __CompanyName__. All rights reserved.
//

#import <OpenGL/glu.h>
#import "FlurryView.h"
#import "Gl_saver.h"

typedef enum _ColorModes
{
	redColorMode,
	magentaColorMode,
	blueColorMode,
	cyanColorMode,
	greenColorMode,
	yellowColorMode,
	slowCyclicColorMode,
	cyclicColorMode,
	tiedyeColorMode,
	rainbowColorMode,
	whiteColorMode,
	multiColorMode,
	darkColorMode
} ColorModes;

extern ColorModes currentColorMode;
extern float incohesion;
extern float colorIncoherence;
extern float streamSpeed;
extern float fieldCoherence;
extern float fieldSpeed;
extern int numParticles;
extern int starSpeed;
extern float seraphDistance;
extern float streamSize;
extern float streamExpansion;
extern float fieldRange;
extern float streamBias;
extern int numStreams;

@implementation FlurryView

- (id)initWithFrame:(NSRect)frameRect isPreview:(BOOL) preview
{
	NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
	
	numStreams = MAX(5,[defaults integerForKey:@"numStreams"]);
	streamExpansion = MAX(1,[defaults integerForKey:@"streamExpansion"]);
	currentColorMode = [defaults integerForKey:@"currentColorMode"];
	
	_initedGL = NO;
	
	if (self = [super initWithFrame:frameRect isPreview:preview]) {
        NSOpenGLPixelFormatAttribute attribs[] =
        {
			NSOpenGLPFAAccelerated,
			NSOpenGLPFADepthSize, 0,
			NSOpenGLPFAColorSize, 32,
			//NSOpenGLPFAMinimumPolicy,
			NSOpenGLPFAClosestPolicy,
                0
        };
        NSOpenGLPixelFormat *format = 
			[[[NSOpenGLPixelFormat alloc] initWithAttributes:attribs] autorelease]; 
        _view = [[[NSOpenGLView alloc] initWithFrame:NSZeroRect pixelFormat:format] autorelease];
        [self addSubview:_view];
    }

    return self;
}

- (void)dealloc
{
	[_view removeFromSuperview];
    [super dealloc];
}

- (void)setFrameSize:(NSSize)newSize
{
    [super setFrameSize:newSize];
    [_view setFrameSize:newSize];
    [[_view openGLContext] makeCurrentContext];

    glClearColor(0.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
    _initedGL = NO;
}

- (void)startAnimation
{
    NSOpenGLContext *context;
    [super startAnimation];
    context = [_view openGLContext];
    [context makeCurrentContext];
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glFlush();
}

- (void)animateOneFrame
{
	int Width, Height;
	
	Width = (int)[self frame].size.width;
	Height = (int)[self frame].size.height;
	
	[[_view openGLContext] makeCurrentContext];
	
	[_view lockFocus];
	
	if (!_initedGL)
	{
		GLSetupRC();
		
		MakeTexture();
		
		_initedGL = YES;
	}
	
	GLResize(Width, Height);
	
	GLRenderScene();
	
	glFlush();
	
	[_view unlockFocus];
}

- (void)stopAnimation
{
    // Do your animation termination here
    _initedGL = NO;
    [super stopAnimation];
}

- (BOOL) hasConfigureSheet
{
	return YES;
}

- (NSWindow*)configureSheet { 

    [NSBundle loadNibNamed:@"Flurry.nib" owner:self];
	
	[streamCountSlider setIntValue:numStreams];
	[colourMenu selectItemAtIndex:currentColorMode];
	[thicknessSlider setFloatValue:sqrt(streamExpansion)];
	
    return window;
}

- (IBAction)changeColourMode:(id)sender
{
	currentColorMode = [colourMenu indexOfSelectedItem];
}

- (void)closeSheet:(id)sender
{
	NSUserDefaults *defaults = [ScreenSaverDefaults defaultsForModuleWithName:@"Flurry"];
	
	numStreams = [streamCountSlider intValue];
	streamExpansion = [thicknessSlider intValue];
	streamExpansion *= streamExpansion;
	currentColorMode = [colourMenu indexOfSelectedItem];
	
	[defaults setInteger:numStreams forKey:@"numStreams"];
	[defaults setInteger:streamExpansion forKey:@"streamExpansion"];
	[defaults setInteger:currentColorMode forKey:@"currentColorMode"];

	[defaults synchronize];
	
	[NSApp endSheet:window];
}

@end
